package com.mzx.javase.thread.join;

/**
 * 类描述：join意思是加入，表示当前线程加入到其他线程中，使其他正在运行的线程进入到阻塞状态，等当前线程结束后
 *
 * @ClassName JoinTest
 * @Description TODO
 * @Author 马子鑫
 * @Date 2022/8/3 17:18
 * @Version 1.0
 */
public class JoinTest {
    public static void main(String[] args) {
        JoinThread joinThread = new JoinThread();
        joinThread.setName("新建线程");
        joinThread.start();
        try {
            joinThread.join();//子线程加入到主线程中，主线程此时进入到阻塞状态，等待子线程执行完毕
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "运行了第：" + i + "次");
        }
    }
}

class JoinThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "开始运行");
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "运行了第" + i + "次");
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");
    }
}
